<template>
	<div class="search_body">
		<div class="search_input">
			<div class="search_input_wrapper">
				<i class="iconfont icon-sousuo"></i>
				<input type="search" v-model="messages">
			</div>
		</div>
		<div class="search_result">
			<h3>电影/电视剧/综艺</h3>
			<ul>
				<li v-for="(item) in movies" :key="item.id">
					<!-- item.img是setWH的第一个参数url (url,arg) -->
					<div class="img"><img :src="item.img | setWH('128.180')"></div>
					<div class="info">
						<p><span>{{item.nm}}</span><span>{{item.sc}}</span></p>
						<p>A Cool Fish</p>
						<p>{{item.cat}}</p>
						<p>{{item.rt}}</p>
					</div>
				</li>
			</ul>
		</div>
	</div>
</template>

<script>
	import {
		setWH
	} from '../../filter'
	export default {
		data() {
			return {
				messages: "", //搜索的关键字
				movies: [] //搜索到的电影
			}
		},
		methods: {
			//防抖函数
			cancelRequest() {
				if (typeof this.source === 'function') {
					this.source('终止请求')
				}
			}
		},
		watch: {
			 /* 监听message的变化，并加上防止多次请求 */
			messages(newVal) {
				var that = this;//保存this的指向
				//调用方法
				let cityId = this.$store.state.city.id
				this.cancelRequest();//在此调用 防抖策略
				this.axios.get('/search?cityId='+cityId+'&kw=' + newVal, {
					//多次请求的时候 在请求之前触发cancelToken
					cancelToken: new this.axios.CancelToken(function(c) {
						that.source = c;
					})
				}).then(res => {//这里输入你正常的时候要得到的数据
					let movies = res.data.movies
					if (movies) {
						this.movies = res.data.movies.list//将数据更新到data的movies中
					}
				}).catch((err) => {//获取数据失败后的回调
                if (this.axios.isCancel(err)) {
                    console.log('Rquest canceled', err.message); //请求如果被取消，这里是返回取消的message
                } else {
                    console.log(err);
                }
            })
			}
		}
	}
</script>

<style scoped>
	#content .search_body {
		flex: 1;
		overflow: auto;
		padding-bottom: 50px;
	}

	.search_body .search_input {
		padding: 8px 10px;
		background-color: #f5f5f5;
		border-bottom: 1px solid #e5e5e5;
	}

	.search_body .search_input_wrapper {
		padding: 0 10px;
		border: 1px solid #e6e6e6;
		border-radius: 5px;
		background-color: #fff;
		display: flex;
		line-height: 20px;
	}

	.search_body .search_input_wrapper i {
		font-size: 16px;
		padding: 4px 0;
	}

	.search_body .search_input_wrapper input {
		border: none;
		font-size: 13px;
		color: #333;
		padding: 4px 0;
		outline: none;
		margin-left: 5px;
		width: 100%;
	}

	.search_body .search_result {}

	.search_body .search_result h3 {
		font-size: 15px;
		color: #999;
		padding: 9px 15px;
		border-bottom: 1px solid #e6e6e6;
	}

	.search_body .search_result ul {}

	.search_body .search_result li {
		border-bottom: 1px #c9c9c9 dashed;
		padding: 10px 15px;
		box-sizing: border-box;
		display: flex;
	}

	.search_body .search_result .img {
		width: 60px;
		float: left;
	}

	.search_body .search_result .img img {
		width: 100%;
	}

	.search_body .search_result .info {
		float: left;
		margin-left: 15px;
		flex: 1;
	}

	.search_body .search_result .info p {
		height: 22px;
		display: flex;
		line-height: 22px;
		font-size: 12px;
	}

	.search_body .search_result .info p:nth-of-type(1) span:nth-of-type(1) {
		font-size: 18px;
		flex: 1;
	}

	.search_body .search_result .info p:nth-of-type(1) span:nth-of-type(2) {
		font-size: 16px;
		color: #fc7103;
	}
</style>
